package com.lkm.market2_0.m_imple.steelyardserver.server.sendtask;

import com.lkm.frame.P;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.DateHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.javatool.ServerTimeSync;
import com.lkm.market2_0.help.DataEncryptionHelp;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.ReceiveTask;

import java.util.Calendar;
import java.util.Locale;



public class SetDateTask extends AbsSendWriteTask {

    

    public SetDateTask(ITaskExecutor<Object[], Void, Boolean> taskExecutor, TaskManager mTaskManager, IHardwareControler bluetoothSocket, ReceiveTask mReceiveTask) {
        super(taskExecutor, SetDateTask.class.getName(), mTaskManager, bluetoothSocket,mReceiveTask);
    }
    int pass=0;

    @Override
    protected byte onGetRequestCMD(int[] outDataSize) {
        outDataSize[0]=pass;
        return 0x0c;
    }
    @Override
    protected byte[] onGetRequestCMDContent() {
        

        Calendar calendar = DateHelp.getCalendar(Locale.getDefault());
        calendar.setTimeInMillis(ServerTimeSync.getInstance().getServerCurTime());

        String y=String.valueOf(calendar.get(Calendar.YEAR)).substring(2);
        int m=calendar.get(Calendar.MONTH)+1;
        int w=calendar.get(Calendar.DAY_OF_WEEK)-1;
        int d=calendar.get(Calendar.DAY_OF_MONTH);

        int h=calendar.get(Calendar.HOUR_OF_DAY);

        int f=calendar.get(Calendar.MINUTE);
        int s=calendar.get(Calendar.SECOND);

        pass = NumberHelp.bytesToIntDesc(DataEncryptionHelp.ToAscii(new byte[]{(byte)Integer.parseInt((y),16)},1));

        return DataEncryptionHelp.ToAscii(new byte[]{

                (byte)Integer.parseInt("0"+(w==0?7:w),16),
                (byte)Integer.parseInt(""+(m<10?"0":"")+m,16),
                (byte)Integer.parseInt(""+(d<10?"0":"")+d,16),
                (byte)Integer.parseInt(""+(h<10?"0":"")+h,16),
                (byte)Integer.parseInt(""+(f<10?"0":"")+f,16),
                (byte)Integer.parseInt(""+(s<10?"0":"")+s,16),







                0,0,0,
                0,0,0,
                0,0,0

        },15);


    }


    @Override
    protected byte[] onGetSendDataItem(int index) {
        return null;
    }


    @Override
    public void onPreExecute() {
        super.onPreExecute();

        if(P.isDebug()) {
            P.p("=======开始设置台秤时间");
            if(UIP.isPrint()){
                UIP.p("Send设置台秤时间：开始",mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
    @Override
    public void onCancel() {
        super.onCancel();

        if(P.isDebug()) {
            P.p("=======设置台秤时间 取消了");
            if(UIP.isPrint()){
                UIP.p("Send设置台秤时间：取消",mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
    @Override
    public void onFinish(Boolean aBoolean) {
        super.onFinish(aBoolean);

        if(P.isDebug()) {
            P.p("======设置台秤时间是否成功："+aBoolean);
            if(UIP.isPrint()){
                UIP.p("Send设置台秤时间：成功？"+aBoolean,mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
}